//
//  MatchedDoctorController.m
//  QuickCure1
//
//  Created by Haozhong Chen on 2016/11/3.
//  Copyright © 2016年 maoge. All rights reserved.
//
#import "MatchedDoctorController.h"
#import "YYModel.h"
#import "NetWorkTool.h"


//界面相同,直接使用关注医生列表的cell和model
#import "FocusDoctorCell.h"
#import "FocusDoctorData.h"


#import "DoctorDetailController.h"


@interface MatchedDoctorController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSMutableArray<FocusDoctorData*>*dataList;

@property(nonatomic,weak)UITableView*tableView;

@property(nonatomic,strong)NSIndexPath*index;
@property(nonatomic,strong)UIAlertController *alertVC;

@end


static NSString*cellID=@"DoctorListCellID";


@implementation MatchedDoctorController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self loadData];
    
}


#pragma mark - 初始化界面
-(void)setupUI
{
    self.title=@"选择医生";
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    NSDictionary*dict=@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]};
    [self.navigationController.navigationBar setTitleTextAttributes:dict];
    
    
    UITableView *table = [[UITableView alloc] init];
    self.tableView = table;
    
    [self.view addSubview:table];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //自动行高
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 80;
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"FocusDoctorCell" bundle:nil] forCellReuseIdentifier:cellID];
    
    [table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}


#pragma mark - 删除关注医生列表的网络请求

-(void)test14{
    //    请求地址: http://iosapi.itcast.cn/deleteDoctor.json.php
    //    请求参数:{"user_id":1000089,"doctor_id":300000315}
    
    NSString *urlString = kDeleteDoctorUrl;
    NSDictionary *paramDic = @{
                               @"user_id":@"1000089",
                               @"doctor_id":@300000315
                               };
    
    //    [self loadUrl:urlString params:paramDic];
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
        NSLog(@"responseObject = %@",responseObject);
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
    
}

#pragma mark - 初始化数据模型列表
-(NSMutableArray<FocusDoctorData*>*)dataList
{
    if (!_dataList)
    {
        _dataList=[[NSMutableArray alloc]init];
        
    }
    return _dataList;
}


#pragma mark - 获取关注医生的接口
-(void)loadData{
    
    NSString *urlString = kMatchDoctors;
    NSDictionary *paramDic = @{@"ci1_id":@1,
                               @"ci2_id":@3,
                               @"ci3_id":@3,
                               @"diagnosis_type":@0,
                               @"page_size":@15,
                               @"is_confirmed":@1,
                               @"user_id":@1000089,
                               @"page":@1,
                               @"has_diagnosis":@2
                               };
    
    
    [self loadUrl:urlString params:paramDic];
    
    
    
}


#pragma mark - 获取网络数据
-(void)loadUrl:(NSString *)urlString
        params:(NSDictionary *)paramDic{
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
        
        NSDictionary*dict=responseObject;
        
        
        
        NSArray*array=dict[@"data"];
        NSArray<FocusDoctorData *> *modelArray = [NSArray yy_modelArrayWithClass:[FocusDoctorData class] json:array];
        
        
        [self.dataList addObjectsFromArray:modelArray];
        [self.tableView reloadData];
        
        
        
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
    
}



#pragma mark tableView的代理方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //model
    FocusDoctorData *model = self.dataList[indexPath.row];
    
    NSString *identifier = cellID;
    
    
    FocusDoctorCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    cell.dataModel =model;
    
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    NSLog(@"%@",self.dataList[1]);
    return  self.dataList.count;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    FocusDoctorData *model = self.dataList[indexPath.row];
    self.index=indexPath;
    
    DoctorDetailController *detailVc = [[DoctorDetailController alloc] init];
    
    detailVc.data=model;
    
    
    [self.navigationController pushViewController:detailVc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
